<?php
namespace app\index\controller;
use think\Controller;

class Base extends Controller
{

    public $configs;
    public function _initialize()
    {
    	$this->_getFooterArts();//获取并分配底部帮助信息
        $this->_getComCates();
        $this->_getNav();
        $this->_getConfs();
        $this->_getCates();
        $this->_userInfo();
        
        
    }
    //获取商品分类二级信息
    public function _getCates(){
        

        $CategoryRes=model('TpCategory')->getCates();
        // dump($CategoryRes);die;
        $this->assign(
         [
             'CategoryRes'=>$CategoryRes
             
         ]
     );

    }

//获取文章模块信息
    private function _getFooterArts(){
// cache('helpCateRes',null);
        if(cache('helpCateRes')){
            $helpCateRes=cache('helpCateRes');
        }else{
            $helpCateRes=model('TpArticle')->getFooterArts();//底部帮助信息文章
            if($this->configs['cache_button']=='是'){
                cache('helpCateRes', $helpCateRes,$this->configs['cache_time']);//设置缓存
            }

        }

        if(cache('artArr')){
            $artArr=cache('artArr');
        }else{
            $artArr=model('TpArticle')->getShopInfo();//网店信息文章
            if($this->configs['cache_button']=='是'){
                cache('artArr', $artArr,$this->configs['cache_time']);//设置缓存
            }

        }

        $this->assign(
         [
             'helpCateRes'=>$helpCateRes, 
             'artArr'=>$artArr
             
         ]
     );

    }


//获取文章模块普通分类
    private function _getComCates(){
        if(cache('ComCateRes')){
            $ComCateRes=cache('ComCateRes');
        }else{
            $ComCateRes=model('TpCate')->getComCates();
            if($this->configs['cache_button']=='是'){
                cache('ComCateRes', $ComCateRes,$this->configs['cache_time']);//设置缓存
            }

        }
        
        $this->assign(
            [
                'ComCateRes'=>$ComCateRes
            ]);
    }


//获取头部导航栏内容
    private function _getNav(){
        if(cache('NavRes')){
            $NavRes=cache('NavRes');
        }else{
            $_NavRes=db('tp_nav')->select();
            $NavRes=array();

            foreach ($_NavRes as $k => $v) {
                $NavRes[$v['pos']][]=$v;
            }
            if($this->configs['cache_button']=='是'){
                cache('NavRes', $NavRes,$this->configs['cache_time']);//设置缓存
            }


        }
        
        $this->assign(
            [
                'NavRes'=>$NavRes
            ]);
        // dump($NavRes);die;

    }

//获取配置项信息
    public function _getConfs(){
        if(cache('configs')){
            $configs=cache('configs');
        }else{
            $configs=model('TpConf')-> getConfs();
            if($this->configs['cache_button']=='是'){
                cache('configs', $configs,$this->configs['cache_time']);//设置缓存
            }


        }
        $this->configs=$configs;
        
        $this->assign(
            [
                'configs'=>$configs
            ]);
    }


        public function _userInfo(){
            $uid=session('uid');
            $userInfo=db('tp_user')->alias('u')->field('u.*,ml.level_name')->join('tp_member_level ml','ml.bom_point<u.points < ml.top_point')->where('u.id',$uid)->find();
            
            $this->assign('userInfo',$userInfo);
        }
    
}


